package com.myCompany.edu.realtime.util;

import com.myCompany.edu.realtime.annotation.NoNeedSink;
import org.apache.flink.shaded.guava18.com.google.common.base.CaseFormat;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author
 * @date 2022/9/19 10:16
 */

public class AtguiguUtil {

    public static String toDate(Long ts) {
        return new SimpleDateFormat("yyyy-MM-dd").format(ts);
    }

    public static String toDateTime(Long ts) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(ts);
    }

    public static List<String> ikSplit(String s) {
        List<String> result = new ArrayList<>();
        //ik 两种分词模式： smart： 智能 ，  max_word： 最多单词
        // 我是中国人
        // s -> Reader
        // 内存流
        StringReader reader = new StringReader(s);
        IKSegmenter seg = new IKSegmenter(reader, true);

        try {
            Lexeme next = seg.next();
            while (next != null) {
                result.add(next.getLexemeText());
                next = seg.next();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return distinct(result);
    }

    //返回一个去重后的list集合
    private static List<String> distinct(List<String> list) {
        return new ArrayList<>(new HashSet<>(list));
    }

    public static <T> List<String> getClassFeildsName(Class<T> tClass) {
//        Field[] fields = tClass.getDeclaredFields();
//        ArrayList<String> list = new ArrayList<>();
//        for (int i = 0; i < fields.length; i++) {
//        list.add(fields[i].getName());
//        }
//       return list;

        Field[] fields = tClass.getDeclaredFields();
        return Stream
                .of(fields)
                .filter(field -> {
                    //有注解就不写，没有注解才写
                    NoNeedSink noNeedSink = field.getAnnotation(NoNeedSink.class);
                    return noNeedSink == null;
                })
                .map(f -> {
                    String name = f.getName();
                    return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name);
                })
                .collect(Collectors.toList());

    }












    public static void main(String[] args) {
        System.out.println(ikSplit("手机华为手机256g手机"));
    }


    public static Long dateToTs(String date) throws ParseException {
        return new SimpleDateFormat("yyyy-MM-dd").parse(date).getTime();
    }

    public static long dateTimeToTs(String create_time) throws ParseException {

        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(create_time).getTime();
    }

    /**
     *
     * @param current  当前时间
     * @param pre   状态中保存的上一次的时间
     * @return 两次时间的对比的大小
     */
    public static boolean isGreater(String current, String pre) {
        current = current.replaceAll("Z", "");
        pre = pre.replaceAll("Z", "");

        return current.compareTo(pre) > 0;
    }
}
